
var initData = function(num){
	var url = base+"/projectCenter/designcell/getFormulaListPage";
	//debugger
	var param = {
			page_num:num,
            page_size:"10",
            project_id:project_id,
            design_cell_id:design_cell_id,
        	formula_table_code:formula_table_code
	};
	$.ajax({
        url: url,
        data: param,
        success: function (data, textStatus, jqXHR) {
        	//debugger
        	if(data != null){
	        	var pageCount = data.dataList.pager.pageCount;
	        	//debugger
	        	var dataObj ={
	        			"list":data.dataList.list,
	         			"dataTool":{
	         				"title":{
                               // "id":"id",
                                "formula_table_code":"公式集编号",
                                "formula_name":"公式名称",
                                //"formula_code":"公式编码",
                                //"constant_code": "常量",
                                //"variable_code":"变量",
                                //"expression":"表达式",
                                "description":"描述",
                            	"operation":"操作"
	            			},
	            			"titleCheckbox":false,
							"btn":[{"event":"updateObj","name":"编辑"}]
						}
		  		};
	        	showPageTable('view', dataObj,num,pageCount,"initData");
        	}
        },
        error: function () {
        }
    });
	
}

//编辑公式集信息
var updateObj = function(obj){
    var contentDIV = function(){
        var content;
        var data = {
            "title":{
                "id":"主键",
                "constant_code":"指定常量",
                "variable_code":"指定变量",
                "formula_code":"公式编号",
                "formula_name":"公式全称",
                "expression":"指定表达式",
                "description":"描述"
            },"text":{
                "id":obj.id,
				"formula_version_id":obj.formula_version_id,
				"formula_table_code":obj.formula_table_code,
                "constant_code":obj.constant_code,
                "variable_code":obj.variable_code,
                "formula_code":obj.formula_code,
                "formula_name":obj.formula_name,
                "expression":obj.expression,
                "description":obj.description,
            },"input":{
                "id":"hidden",
                "formula_version_id":"hidden",
                "formula_table_code":"hidden",
                "constant_code":"text",
                "variable_code":"text",
                "formula_code":"text",
                "formula_name":"text",
                "expression":"text",
                "description":"text"
            },"input_init":{
            }
        };
        var getDiv = dc_update.innerHTML;
        laytpl(getDiv).render(data,function(html){
            content = html;
        });
        return content;
    }
    layopen(contentDIV());
}

//验证数据
var verificationData = function(data){
    if(data.constant_code == ""){
        var obj = $("input[name='constant_code']");
        obj.focus();
        layer.tips('请填写此字段!', obj);
        return false;
    }
    if(data.variable_code == ""){
        var obj = $("input[name='variable_code']");
        obj.focus();
        layer.tips('请填写此字段!', obj);
        return false;
    }
    if(data.formula_code == ""){
        var obj = $("input[name='formula_code']");
        obj.focus();
        layer.tips('请填写此字段!', obj);
        return false;
    }
    if(data.formula_name == ""){
        var obj = $("input[name='formula_name']");
        obj.focus();
        layer.tips('请填写此字段!', obj);
        return false;
    }
    if(data.expression == ""){
        var obj = $("input[name='expression']");
        obj.focus();
        layer.tips('请填写此字段!', obj);
        return false;
    }
    if(data.description == ""){
        var obj = $("input[name='description']");
        obj.focus();
        layer.tips('请填写此字段!', obj);
        return false;
    }
    return true;
}

//将数据渲染到弹窗
var layopen = function(content){
    layer.open({
        title:'更新公式'
        ,content: content
        ,area: ['800px', '600px']
        ,scrollbar: false
        ,btn: ['确认', '取消']
        ,yes: function(index,layero){ //确认
            var data = $(layero).find("form").serializeJSON();
            if(!verificationData(data)){//验证失败!
                return false;
            }
            $.post(base+"/projectCenter/designcell/updateFormula",{
                    formula_version_id:data.formula_version_id,
                    id:data.id==null?"":data.id,
                    constant_code:$.trim(data.constant_code),
                    variable_code:$.trim(data.variable_code),
                    formula_code:$.trim(data.formula_code),
                    formula_name:$.trim(data.formula_name),
                    expression:data.expression,
                    description:data.description,
					formula_table_code:data.formula_table_code,
                    project_id:project_id
                },
                function (data,status){
                    if(data!=""&&data!=null){
                        if(data.statusCode == 0){
                            layer.msg('更新成功', {icon: 1,time:2000});
                        }else{
                            layer.msg(data.message, {icon: 2,time:2000});
                        }
                    }else{
                        layer.msg('更新失败!', {icon: 2,time:2000});
                    }
                    initData(1);
                })
        }
    });
}

var formula_table_code = $("#formula_table_code").val();

var back=function(){
    //history.back(-1);
	var url = base+"/projectCenter/designcell/formulaIndex/"+design_cell_id+"/"+project_id+"/"+data_id;
     $.get(url, function(result){
  	   $(".layui-show").html("");
  	   $(".layui-show").html(result);
     });
}
var data_id = $.trim($("#data_id").val());

var showButton = function(){
    var button_html = ' <div class="btn-team">'+
        ' <button class="btn btn-default" id="back" onclick="back()">返回</button>';
    $(".btn-team").remove();
    $("#div_content").prepend(button_html);
}

var init_select = function(){
	$("[data_id='"+data_id+"']").removeClass(".layui-this").addClass(".layui-this");
}

$(document).ready(function(){
    showButton();
    init_select();
	initData(1);
});